package com.fhz.day016;

import android.animation.TypeEvaluator;
import android.animation.ValueAnimator;
import android.content.Context;
import android.graphics.PointF;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import androidx.annotation.Nullable;
import androidx.core.content.ContextCompat;

import java.util.Random;

/**
 * 时间:2022/6/5
 *
 * @author msfeng
 * 简述: TODO
 */
public class MyView extends RelativeLayout {

    private int width = 0;
    private int height = 0;

    private Drawable[] drawables = new Drawable[4];
    private Object MyEvaluator;

    public MyView(Context context) {
        this(context,null);
    }
    public MyView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    private void init() {
        drawables[0] = ContextCompat.getDrawable(getContext(),R.mipmap.ic_launcher);
        drawables[1] = ContextCompat.getDrawable(getContext(),R.mipmap.ic_launcher);
        drawables[2] = ContextCompat.getDrawable(getContext(),R.mipmap.ic_launcher);
        drawables[3] = ContextCompat.getDrawable(getContext(),R.mipmap.ic_launcher);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
         width = getMeasuredWidth();
         height = getMeasuredHeight();
    }

    public void addImageView(){
        ImageView imageView = new ImageView(getContext());
        imageView.setImageDrawable(drawables[new Random().nextInt(drawables.length)]);
        addView(imageView);

        //设置一个动画
        getAnimator(imageView).start();
        //设置动画
    }

    private ValueAnimator getAnimator(ImageView imageView) {

        //1,开始点.2结束点

        PointF start = new PointF(width/2,height);
        PointF endPointF = new PointF(new Random().nextInt(width), new Random().nextInt(50));
        MyView.MyEvaluator myEvaluator = new MyEvaluator(getPointF(),getPointF());
        ValueAnimator valueAnimator = ValueAnimator.ofObject(myEvaluator, start, endPointF);
        valueAnimator.setDuration(3000);
        valueAnimator.setTarget(imageView);
        valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                PointF pointF = (PointF) animation.getAnimatedValue();
                imageView.setX(pointF.x);
                imageView.setY(pointF.y);

                //
                imageView.setAlpha(1-animation.getAnimatedFraction());
            }
        });

        return valueAnimator;
    }

    public PointF getPointF(){
        PointF pointF = new PointF();
        pointF.x = (float) (Math.random()*width);
        pointF.y = new Random().nextFloat()*height/4;
        return pointF;
    }
    public class MyEvaluator implements TypeEvaluator<PointF>{

        private PointF p1;
        private PointF p2;


        public MyEvaluator(PointF p1, PointF p2) {
            this.p1 = p1;
            this.p2 = p2;
        }

        @Override
        public PointF evaluate(float fraction, PointF startValue, PointF endValue) {
             float  leftTime = 1-fraction;
            PointF pointF = new PointF();

            pointF.x = leftTime * leftTime * leftTime * startValue.x
                    + leftTime * leftTime * 3 * fraction * p1.x
                    + leftTime * 3 * fraction * fraction * p2.x
                    + fraction * fraction * fraction * endValue.x;

            pointF.y = leftTime * leftTime * leftTime * startValue.y
                    + leftTime * leftTime * 3 * fraction * p1.y
                    + leftTime * 3 * fraction * fraction * p2.y
                    + fraction * fraction * fraction * endValue.y;

            return pointF;
        }
    }
}

